<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <#list  columns as field>
        <el-form-item label="${field.columnComment!}" prop="${field.columnName!}">
          <el-input
                  v-model="queryParams.${field.columnName!}"
                  placeholder="请输入${field.columnComment!}"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </#list>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <msx-table ref="table" :query="queryParams" :api="url.page" :columns="this.columns"
              @selection-change="handleSelectionChange"

    >
      <#list  columns as field>
        <#if field.javaType=="Date">
          <template slot="${field.columnName!}" slot-scope="scope">
            <span>{{ parseTime(scope.row.${field.columnName!}, '{y}-{m}-{d}') }}</span>
          </template>
        </#if>
      </#list>
      <template slot="action" slot-scope="scope">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
        >修改
        </el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
        >删除
        </el-button>
      </template>
    </msx-table>
    <${table\-name!}-dialog @close="close" :infoId="this.upodate${KeyName!}" :title="title" :visible.sync="open"></${table\-name!}-dialog>
  </div>
</template>

<script>
  import msxTable from "@/views/components/msxTable";
  import ${TableName!}Dialog from "./dialog/${TableName!}Dialog";
  import {getAction, jsonAction, postAction} from "@/api/action";
  export default {
    name: "${TableName!}",
    components: {msxTable, ${TableName!}Dialog},
    data() {
      return {
        columns: [
          {
            type: "selection",
            width: "55",
            align: "center",
          },
          <#list  columns as field>
          {
            label: "${field.columnComment!}",
            align: "center",
            prop: "${field.columnName!}",
            <#if field.javaType=="Date">
            slot: "${field.columnName!}",
            </#if>
          },
          </#list>
          {
            label: "操作",
            width: "180",
            slot: "action",
            className: "small-padding fixed-width"
          }
        ],
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        upodate${KeyName!}: "",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        url:{
          add:"/${tableName!}/baseAdd",
          del:"/${tableName!}/baseRemove",
          page:"${tableName!}/page",
          update:"/${tableName!}/update"
        }
      };
    },
    created() {
    },
    methods: {
      /** 查询${tableComment!}列表 */
      getList() {
        this.$refs["table"].reload()
      },
      // 关闭取消按钮
      close() {
        //dialog通过监视这个来触发更新的初始化，需要置空
        this.upodate${KeyName!}="";
        this.open = false;
        this.getList()
      },
      // 表单重置
      /** 搜索按钮操作，reset返回到第一页 */
      handleQuery() {
        this.$refs["table"].reset()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs["queryForm"].resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.${keyName!})
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.open = true;
        this.title = "添加${tableComment!}";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.upodate${KeyName!} = row.${keyName!} || this.ids[0]
        this.open = true;
        this.title = "修改${tableComment!}";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              postAction(this.url.update,this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              postAction(this.url.add,this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids =  row.${keyName!}||this.ids.join(",");
        this.$modal.confirm('是否确认QQ服务编号为"' + ids + '"的数据项？').then( ()=> {
          return postAction(this.url.del,{ids:ids});
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch((e) => {
          console.error(e)
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.$download('/${tableName!}/export', {
          ...this.queryParams
        }, `${tableComment!}_${r"${new Date().getTime()}"}.xlsx`)
      }
    }
  };
</script>
